package com.example.demo;

import com.example.demo.mapper.EmpMapper;
import com.example.demo.model.Emp;
import com.example.demo.model.EmpExample;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@Slf4j
@MapperScan("model")
public class DemoApplication implements CommandLineRunner {


	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	private void generateArtifacts() throws Exception {
		List<String> warnings = new ArrayList<>();
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration config = cp.parseConfiguration(
				this.getClass().getResourceAsStream("/generatorConfig.xml"));
		DefaultShellCallback callback = new DefaultShellCallback(true);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
		myBatisGenerator.generate(null);
	}

	@Autowired
	EmpMapper empMapper;
	@Override
	public void run(String... args) throws Exception {
//		generateArtifacts();
		log.info("out");
		Emp emp = Emp.builder().name("张三").job("主管")
				.salary(Money.of(CurrencyUnit.of("CNY"), 20.0)).build();
		empMapper.insert(emp);
		log.info(empMapper.selectByPrimaryKey(3L).toString());
		EmpExample empExample = new EmpExample();
		empExample.createCriteria().andIdEqualTo(2L);
		empMapper.selectByExample(empExample).forEach(e->log.info("after = "+e.toString()));
	}



}
